# -*- coding: utf-8 -*-
import traceback
from utils.backcode import ImportError
from app.manage.financeManage.common.manage_sql import FinanceManageSql
from utils.logger import Md_Debug
from utils.db_utils import DbConn
import datetime

class FinanceEditDB(object):
    def __init__(self):
        self.db = DbConn()
        self.now = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d")
        self.time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
    
    def add_finance(self, body):
        """新增任务"""
        try:
            ret = dict()
            finance_task_name = body.get("finance_task_name")
            finance_task_describe = body.get("finance_task_describe")
            finance_task_header = body.get("finance_task_header")
            finance_task_member = body.get("finance_task_member")
            finance_task_present = body.get("finance_task_present")
            finance_create_user = body.get("finance_create_user")
            finance_plan_start = body.get("finance_plan_start")
            finance_plan_end = body.get("finance_plan_end")
            
            # check parameter
            if not finance_task_name:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_name is none"
            elif not finance_task_describe:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_describe is none"
            elif not str(finance_task_header):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_header is none"
            elif not finance_task_member:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_member is none"
            elif not finance_task_present:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_present is none"
            elif not str(finance_create_user):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_create_user is none"
            elif not finance_plan_start:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_plan_start is none"
            elif not finance_plan_end:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_plan_end is none"
            else:
                params = [finance_task_name, 0, finance_task_describe, finance_task_header, finance_task_member,
                          finance_task_present, finance_create_user, self.time, finance_plan_start, finance_plan_end]
                if self.db.commit(FinanceManageSql.ADD_FINANCE, params):
                    code = ImportError.IMPORT_CODE_SUCCESS
                    message = "success"
                else:
                    code = ImportError.UPDATE_GAME_TYPE
                    message = "failed"
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
    
    def edit_task(self, body):
        """编辑任务"""
        try:
            ret = dict()
            finance_id = body.get("finance_id")
            finance_task_name = body.get("finance_task_name")
            finance_task_describe = body.get("finance_task_describe")
            finance_task_header = body.get("finance_task_header")
            finance_task_member = body.get("finance_task_member")
            finance_task_present = body.get("finance_task_present")
            finance_plan_start = body.get("finance_plan_start")
            finance_plan_end = body.get("finance_plan_end")
            
            # check edit_task
            if not str(finance_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_id is none"
            elif not finance_task_name:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_name is none"
            elif not finance_task_describe:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_describe is none"
            elif not finance_task_header:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_header is none"
            elif not finance_task_member:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_member is none"
            elif not finance_task_present:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_task_present is none"
            elif not finance_plan_start:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_plan_start is none"
            elif not finance_plan_end:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_plan_end is none"
            else:
                params = [finance_task_name, finance_task_describe, finance_task_header, finance_task_member,
                          finance_task_present, finance_plan_start, finance_plan_end, finance_id]
                if self.db.commit(FinanceManageSql.EDIT_TASK, params):
                    code = ImportError.IMPORT_CODE_SUCCESS
                    message = "success"
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
    
    def add_finance_report(self, body):
        """编写日报"""
        try:
            ret = dict()
            # 添加0、 编辑1
            type = body.get("type")
            finance_id = body.get("finance_id")
            report_classfy_id = body.get("report_classfy_id")
            report_info = body.get("report_info")
            
            # check edit_task
            if not str(finance_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_id is none"
            elif not str(type):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "type is none"
            elif not str(report_classfy_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "report_classfy_id is none"
            elif not report_info:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "report_info is none"
            else:
                if type == 0:
                    report_user_id = body.get("report_user_id")
                    if not str(report_user_id):
                        code = ImportError.IMPORT_CODE_INVALID_ARGS
                        message = "report_user_id is none"
                    else:
                        params = [finance_id, report_classfy_id, report_info, report_user_id, self.time]
                        self.db.commit(FinanceManageSql.ADD_FINANCE_REPORT, params)
                        code = ImportError.IMPORT_CODE_SUCCESS
                        message = "success"
                elif type == 1:
                    params = [report_info, finance_id, report_classfy_id]
                    self.db.commit(FinanceManageSql.UPDATE_FINANCE_REPORT, params)
                    code = ImportError.IMPORT_CODE_SUCCESS
                    message = "success"
                else:
                    pass
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret

    def edit_task_status(self, body):
        """开启任务(修改任务状态)"""
        try:
            ret = dict()
            finance_id = body.get("finance_id")
            finance_status = body.get("finance_status")
        
            # check parameter
            if not str(finance_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_id is none"
            elif not str(finance_status):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_status is none"
            else:
                self.db.commit(FinanceManageSql.START_FINANCE, [finance_status, finance_id])
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
